<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script type="text/javascript">
  /////////
    //递归深拷贝
    const oldobj = {
      name: 'xiaming',
      age: '20',
      colors: ['red', 'blue', 'black'],
      frind: {
        name: 'xiaohong'
      }
    }

    function copy(obj) {
      let newObj = {};
      for (let key in obj) {
        if (obj[key] instanceof Object) {
          
        } else {
          newObj[key] = obj[key];
        }
        newObj[key] = copy(obj[key]);
      }

      return newObj;
    }
    const newobj = copy(oldobj);

    newobj.frind.name = 'xxx';
    console.log(oldobj)
    console.log(newobj)
    /////////////////////////////////////////////
    //json深拷贝
    const oldobj = {
      name: 'xiaming',
      age: '20',
      colors: ['red', 'blue', 'black'],
      frind: {
        name: 'xiaohong'
      }
    }
    // let json = JSON.stringify(oldobj)
    // console.log(json)//对象转json
    // let obj = JSON.parse(json)
    // console.log(obj)
    function copy(obj){
      let json = JSON.stringify(obj)
      let newObj = JSON.parse(json)
      return newObj;
    }

    let newObj = copy(oldobj)
    newObj.frind.name = 'xxxx'
    console.log(oldobj)
    console.log(newObj)


    


    /////////////////////////////////////////////////
    /*
        typeof对于基本数据类型判断是没有问题的，但是遇到引用数据类型（如：Array）是不起作用的。
    */
    //浅拷贝
    /*
    function shallowCopy(object) {
        if (!object || typeof object !== 'object')
            return;
        var newObject = Array.isArray(object) ? [] : {};
        for (let i in object) {
            if (object.hasOwnProperty(i)) {
                newObject[i] = object[i];
            }
        }
        return newObject;
    }
    var a = {
        id: 1,
        msg: {
            age: 18
        }
    }
    var b = {};
    Object.assign(b, a);
    // var b = shallowCopy(a);
    b.msg.age = 123;
    console.log(a);
    //深拷贝
    function deepCopy(object) {
        if (!object || typeof object !== 'object')
            return;
        var newObject = Array.isArray(object) ? [] : {};
        for (let i in object) {
            if (object.hasOwnProperty(i)) {
                if (typeof object[i] == 'object') {
                    newObject[i] = deepCopy(object[i]);
                } else {
                    newObject[i] = object[i];
                }
            }
        }
        return newObject;
    }
    var c = deepCopy(a);
    c.msg.age = 43;
    console.log(a);*/


    function shallowCopy(object) {
      if (!object || typeof object != 'object')
        return;
      var newObject = Array.isArray(object) ? [] : {};
      for (var i in object) {
        if (object.hasOwnProperty(i)) {
          newObject[i] = object[i]
        }
      }
      return newObject;
    }
    var a = {
      1: 12,
      msg: {
        age: 18
      }
    }
    // var b = shallowCopy(a);
    // console.log(b);
    // b.msg.age = 123;
    // console.log(a);
    function deepCopy(object) {
      if (!object || typeof object != 'object') {
        return object;
      }
      var newObject = Array.isArray(object) ? [] : {};
      for (let i in object) {
        if (object.hasOwnProperty(i)) {
          if (typeof (object[i]) == 'object') {
            newObject[i] = deepCopy(object[i]);
          } else {
            newObject[i] = object[i];
          }
        }
      }
      return newObject;
    }
    var b = deepCopy(a);

    ///////////////////////////
  </script>
</body>

</html>